package dk.rafaelcouto.PhoneTone_Extractor;

import java.util.*;

public class Ai
{
    private char lastTone;
    private char lastTone1;
    private char lastTone2;
    private ArrayList lista;
    private ArrayList lista2;
    private int[] results;
    private boolean silence;
    private boolean start;
    private boolean stop;
    private char tone;
    
    public Ai() {
        this.lista = new ArrayList();
        this.lista2 = new ArrayList();
        this.lastTone = ' ';
        this.lastTone1 = ' ';
        this.lastTone2 = ' ';
        this.tone = ' ';
        this.silence = true;
        this.results = new int[8];
        this.start = false;
        this.stop = false;
    }
    
    private int calculamedia(final char c, final int[] array) {
        int n = 0;
        int n2 = 0;
        int n3 = 0;
        int n4 = 0;
        if (c == 'l') {
            for (int i = 0; i < 4; ++i) {
                if (n < array[i]) {
                    n = array[i];
                }
                n3 += array[i];
            }
            return (n3 - n) / 3;
        }
        if (c == 'h') {
            for (int j = 4; j <= 7; ++j) {
                if (n2 < array[j]) {
                    n2 = array[j];
                }
                n4 += array[j];
            }
            return (n4 - n2) / 3;
        }
        return 0;
    }
    
    public void addTone(final ToneProcessor toneProcessor) {
        if (toneProcessor instanceof ToneProcessor) {
            this.lista.add(toneProcessor);
        }
    }
    
    public char ai(final ToneProcessor toneProcessor) {
        char tone = 'S';
        this.lista.add(toneProcessor);
        if (this.lista.size() > 3 && this.lastTone2 == this.lastTone1 && (this.lastTone1 != toneProcessor.tone || this.silence)) {
            final ToneProcessor toneProcessor2 = (ToneProcessor)this.lista.get(this.lista.size() - 2);
            this.lastTone = toneProcessor.tone;
            tone = toneProcessor.tone;
        }
        else if (this.lastTone != toneProcessor.tone) {
            this.silence = true;
            return tone;
        }
        return tone;
    }
    
    public void clear() {
        this.lastTone = ' ';
        this.lista.clear();
        this.lista2.clear();
    }
    
    public char decide2(final int n, final int[] results) {
        int n2 = results[0];
        int n3 = results[4];
        int n4 = 0;
        int n5 = 0;
        int n6 = 0;
        this.results = results;
        char lastTone = ' ';
        final int calculamedia = this.calculamedia('l', results);
        final int calculamedia2 = this.calculamedia('h', results);
        for (int i = 0; i < 4; ++i) {
            if (n2 < results[i]) {
                n4 = n2;
                n2 = results[i];
            }
            n6 += results[i];
        }
        for (int j = 4; j <= 7; ++j) {
            if (n3 < results[j]) {
                n5 = n3;
                n3 = results[j];
            }
            n6 += results[j];
        }
        final int n7 = n * (n6 / 8) / 100;
        if (n2 < n4 * n / 100 || n3 < n5 * n / 100 || n7 > (n2 + n3) / 2 || n2 < calculamedia * n / 100 || n3 < calculamedia2 * n / 100) {
            lastTone = ' ';
            this.silence = true;
            this.stop = true;
            this.start = false;
        }
        else if (n2 / 3 <= n3 || n2 >= n3 / 3) {
            if (n2 == results[0] && n3 == results[4]) {
                lastTone = '1';
            }
            else if (n2 == results[0] && n3 == results[5]) {
                lastTone = '2';
            }
            else if (n2 == results[0] && n3 == results[6]) {
                lastTone = '3';
            }
            else if (n2 == results[0] && n3 == results[7]) {
                lastTone = 'A';
            }
            else if (n2 == results[1] && n3 == results[4]) {
                lastTone = '4';
            }
            else if (n2 == results[1] && n3 == results[5]) {
                lastTone = '5';
            }
            else if (n2 == results[1] && n3 == results[6]) {
                lastTone = '6';
            }
            else if (n2 == results[1] && n3 == results[7]) {
                lastTone = 'B';
            }
            else if (n2 == results[2] && n3 == results[4]) {
                lastTone = '7';
            }
            else if (n2 == results[2] && n3 == results[5]) {
                lastTone = '8';
            }
            else if (n2 == results[2] && n3 == results[6]) {
                lastTone = '9';
            }
            else if (n2 == results[2] && n3 == results[7]) {
                lastTone = 'C';
            }
            else if (n2 == results[3] && n3 == results[4]) {
                lastTone = '*';
            }
            else if (n2 == results[3] && n3 == results[5]) {
                lastTone = '0';
            }
            else if (n2 == results[3] && n3 == results[6]) {
                lastTone = '#';
            }
            else if (n2 == results[3] && n3 == results[7]) {
                lastTone = 'D';
            }
            this.silence = false;
            this.start = true;
            this.stop = false;
            int n8 = 0;
            for (int length = results.length, k = 0; k < length; ++k) {
                n8 += results[k];
            }
            if (n8 < n * 1000) {
                this.silence = true;
            }
        }
        final ToneProcessor toneProcessor = new ToneProcessor(lastTone, this.results);
        if (this.lastTone == this.lastTone1 && (this.silence || toneProcessor.tone != this.lastTone)) {
            toneProcessor.ready = true;
        }
        this.lastTone2 = this.lastTone1;
        this.lastTone1 = this.lastTone;
        this.lastTone = lastTone;
        if (toneProcessor.ready) {
            return this.lastTone1;
        }
        return ' ';
    }
    
    public ToneProcessor getTone(final int n) {
        return (ToneProcessor)this.lista.get(n);
    }
}
